/**
 * @author 黄波
 * 创建日期：2011Oct 11, 2011
 * TODO// N/A
 */
package com.platform.os.bo;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.platform.dao.BaseDao;
import com.platform.pojo.AccessRecord;

/**
 * 客户端访问服务器的业务
 */
public class AccessBo {

	private static AccessBo instance;
	static {
		instance = new AccessBo();
	}
	private AccessBo() {
	}
	
	public static AccessBo getInstance() {
		return instance;
	}
	
	/**
	 * 添加访问记录
	 * @param obj
	 * @return
	 */
	public boolean addAccessRecord(AccessRecord obj) {
		return BaseDao.add(obj);
	}
	
	/**
	 * 根据ip返回当前该IP的访问记录
	 * @param ip
	 * @return
	 */
	public AccessRecord getAccessRecord(String ip) {
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String accessTime = format.format(today);
		return (AccessRecord) BaseDao.getUniqueResult("from AccessRecord where ip='" + ip + "' and accessTime>'" + accessTime + " 00:00:00'");
	}

	/**
	 * 更新访问记录
	 * @param obj
	 * @return
	 */
	public boolean updateAccessRecord(AccessRecord obj) {
		return BaseDao.update(obj);
	}
	
	/**
	 * 计算当天访问记录总数
	 * @return
	 */
	public Long countAccessRecord() {
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String accessTime = format.format(today);
		return (Long)BaseDao.getUniqueResult("select count(*) from AccessRecord where accessTime>'" + accessTime + " 00:00:00'");
	}
}
